import { Modal, Row, Select, message } from "antd";
import { addon, Permission } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import SignFrameLayout, { InputType as ListInputType } from "src/business/components/buss-components/sign-frame-layout";
import { AppServiceUtility } from "src/projects/app/appService";
import { table_param, edit_props_info } from "src/projects/app/util-tool";
import FormCreator, { InputType } from "src/business/components/buss-components/form-creator";
import { GetFieldDecoratorOptions } from "antd/lib/form/Form";
import { ROUTE_PATH } from "src/projects/router";
import { request } from "src/business/util_tool";

const { Option } = Select;
/**
 * 组件：服务订单分派任务列表状态
 */
export interface ServiceOrderTaskViewState extends ReactViewState {
    // 选择行的Id集合
    ids?: string[];
    // 任务项目集合
    task_list?: any[];
    // 任务总数总条数
    total?: number;
    // 弹出框是否显示
    modal_visible?: boolean;
    // id集合
    id?: string;
    // 可分派人员
    task_receive_person?: any;
    // 服务项目类别
    service_item_category?: [];
    // 单个项目
    item?: any;
    /** 接口名 */
    request_url?: string;
}

/**
 * 组件：服务订单分派任务列表
 * 服务订单分派任务列表
 */
export class ServiceOrderTaskView extends ReactView<ServiceOrderTaskViewControl, ServiceOrderTaskViewState> {
    private columns_data_source = [{
        title: '订单编号',
        dataIndex: 'order_code',
        key: 'order_code',
    }, {
        title: '采购人',
        dataIndex: 'purchaser',
        key: 'purchaser',
    }, {
        title: '服务商',
        dataIndex: 'service_provider',
        key: 'service_provider',
    }, {
        title: '订单内容',
        dataIndex: 'origin_product.product_name',
        key: 'origin_product.product_name',
    }, {
        title: '分派状态',
        dataIndex: 'order_state',
        key: 'order_state',
    }, {
        title: '服务状态',
        dataIndex: 'status',
        key: 'status',
    }, {
        title: '订单时间',
        dataIndex: 'order_date',
        key: 'order_date',
    }
        // {
        //     title: '总次数',
        //     dataIndex: 'all_times',
        //     key: 'all_times',
        // }, {
        //     title: '剩余次数',
        //     dataIndex: 'remaining_times',
        //     key: 'remaining_times',
        // }, 
    ];
    constructor(props: any) {
        super(props);
        this.state = {
            task_receive_person: [],
            modal_visible: false,
            service_item_category: [],
            id: '',
            request_url: '',
        };
    }
    /** 模版选择框取消回调方法 */
    handleCancel = (e: any) => {
        this.setState({
            modal_visible: false,
        });
    }

    /** 指派任务 */
    handleSubmit = (e: any, value: any) => {
        let item = this.state.item;
        console.log('<', item);
        if (!item.id || !item.origin_product.product_id || !item.organization_id) {
            message.error('数据出错！');
            return;
        }
        let param: any = {
            // 组装要传入服务记录接口的数据
            record_data: {
                order_id: item.id,
                product_id: item.origin_product.product_id,
                other: {
                    servicer_id: value.task_receive_person,
                    // servicer_id: item.organization_id,
                }
            },
            // 构建要创建任务的接口数据
            task_data: {
                // 任务名称
                'task_name': item.origin_product.product_name,
                // 任务内容
                'task_content': item.origin_product.product_name,
                // 任务派单者
                'task_receive_person': value.task_receive_person,
                // 任务地址
                'task_address': item.address,
                // 任务坐标
                'task_location': item.address,
                // 任务备注
                'remark': item.comment,
                // 主键
                'task_object_id': item.id,
            }
        };
        request(this, AppServiceUtility.service_order_task_service.update_service_order_task_manage!(param))
            .then((data: any) => {
                if (data === 'Success') {
                    this.setState({
                        modal_visible: false,
                    });
                    message.info('操作成功');
                    setTimeout(
                        () => {
                            this.props.history!.push(ROUTE_PATH.serviceOrderTask);
                        },
                        300
                    );
                } else {
                    message.info(data);
                }
            }).catch((error: Error) => {
                message.error('创建失败！' + error.message);
            });
    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
            // condition: [{ personnel_category: personnelCategory.elder }, 1, 10] // 长者资料
        });
    }
    componentDidMount() {
        // 获取服务项目类型
        request(this, AppServiceUtility.service_order_task_service.get_services_item_category_list!()!)
            .then((datas: any) => {
                this.setState({
                    service_item_category: datas,
                });
            });
    }

    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        if (contents.remaining_times < 1) {
            message.info('所选项目没有剩余次数');
            return;
        }
        if ('icon_edit' === type) {
            this.setState({ modal_visible: true, item: contents });
        }
    }
    render() {
        let modal_search_items_props = {
            edit_form_items_props: [
                {
                    type: ListInputType.input,
                    label: "请选择分派人员",
                    decorator_id: "task_receive_person"
                },
            ],
            columns_data_source: [{
                title: '编号',
                dataIndex: 'code',
                key: 'code',
            }, {
                title: '名称',
                dataIndex: 'name',
                key: 'name',
            }, {
                title: '身份证号',
                dataIndex: 'id_card',
                key: 'id_card',
            },],
            service_name: AppServiceUtility.person_org_manage_service,
            service_func: 'get_worker_list_all',
            title: '分派人员查询',
            select_option: {
                placeholder: "请选择分派人员",
            }
        };
        let category = this.state.service_item_category;
        let service_category: any[] = [];
        category!.map((item: any) => {
            service_category.push(<Option key={item['id']}>{item['name']}</Option>);
        });
        let task_option = {
            type_show: false,
            edit_form_items_props: [{
                type: ListInputType.rangePicker,
                label: "订单时间",
                decorator_id: "order_date"
            }],
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.service_operation_service,
            service_option: {
                select: {
                    service_func: this.state.request_url ? this.state.request_url : '',
                    service_condition: [{}, 1, 10]
                },
            },
            searchExtraParam: { need_fp: true },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.edit_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let task_list = Object.assign(task_option, table_param);
        let edit_props = {
            form_items_props: [
                {
                    title: "分派人员",
                    need_card: true,
                    input_props: [
                        {
                            type: InputType.modal_search,
                            label: "分派人员",
                            decorator_id: "task_receive_person",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请选择分派人员" }],
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请选择分派人员",
                                modal_search_items_props: modal_search_items_props
                            }
                        },
                    ]
                }
            ],
            submit_btn_propps: {
                text: "分派",
                cb: this.handleSubmit
            },
            service_option: {
                service_object: AppServiceUtility.service_order_task_service,
                operation_option: {
                    query: {
                        func_name: this.state.request_url ? this.state.request_url : '',
                        arguments: [{ id: this.props.match!.params.key }, 1, 1]
                    },
                },
            },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.edit_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
            succ_func: () => { this.setState({ modal_visible: false, }); },
            id: this.props.match!.params.key,
        };
        let edit_props_list = Object.assign(edit_props, edit_props_info);
        return (
            <Row>
                <Modal
                    visible={this.state.modal_visible}
                    onCancel={this.handleCancel}
                    width={900}
                    okButtonProps={{ disabled: true, ghost: true }}
                    cancelButtonProps={{ disabled: true, ghost: true }}
                >
                    <FormCreator {...edit_props_list} />
                </Modal>
                <SignFrameLayout {...task_list} />
            </Row >
        );
    }
}

/**
 * 控件：服务订单分派任务列表
 * 服务订单分派任务列表
 */
@addon('ServiceOrderTaskView', '服务订单分派任务列表', '服务订单分派任务列表')
@reactControl(ServiceOrderTaskView, true)
export class ServiceOrderTaskViewControl extends ReactViewControl {
    /** 视图权限 */
    public permission?: Permission;
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
}